package com.android.vestel.photoroid;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class ShowImageActivity extends Activity {
	ImageView imageView;
	int index;
	ArrayList<String> ImageURIs;
	Timer t;
	Handler han;
	TimerTask tt;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.imageviewer);

		// initialization
		imageView = (ImageView) findViewById(R.id.imageView1);
		index = 0;
		// ImageURIs = new ArrayList<String>();
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				t.cancel();
				setResult(0);
				finish();
			}
		});
		
		Bundle b = getIntent().getExtras();
		ImageURIs = b.getStringArrayList("ImageURIs"); 
		Log.e("show image activity", ImageURIs.toString());
		
		han = new Handler();
		tt = new TimerTask() {
			@Override
			public void run() {
				Log.e("SlideShowTimertask", "Running");
				han.post(new Runnable() {
					@Override
					public void run() {
						Log.e("SlideShowTimertask", "Next Image");
						//try {
							// image.setImageResource((int)imageIds.get(index));
							imageView.setImageURI(Uri.parse(ImageURIs
									.get(index)));
							index++;
							index %= ImageURIs.size();
//						} catch (Exception e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
					}
				});
			}
		};
		t = new Timer();
		t.schedule(tt, 0, 3000);
		//slideImages();
	}

//	private void slideImages() {
//		
//	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		t.cancel();
		setResult(0);
		finish();
	}

}
